﻿Public Class frmThoiKhoaBieu

    Private Sub frmThoiKhoaBieu_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LayDSTKB()
    End Sub

    Private Sub LayDSTKB()
        Try
            Dim tkbBUS As New ThoiKhoaBieuBUS
            Dim ds As New List(Of ThoiKhoaBieuDTO)
            Dim i As Integer = 1

            ds = tkbBUS.LayDanhSachThoiKhoaBieu()
            dgTKB.Rows.Clear()

            For Each tkb As ThoiKhoaBieuDTO In ds
                dgTKB.Rows.Add(i, tkb.MaTKB, tkb.Thu, tkb.Buoi, tkb.MaLop, tkb.TenLop)
                i += 1
            Next
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub btThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btThem.Click
        Try
            If (txtMaTKB.Text.Trim.Length = 0) Then
                MessageBox.Show("Nhap Ma TKB.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Return
            ElseIf (cbThu.SelectedItem = Nothing) Then
                MessageBox.Show("Chon Thu.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Return
            ElseIf (cbBuoi.SelectedItem = Nothing) Then
                MessageBox.Show("Chon Buoi.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Return
            End If

            Dim tkbDTO As New ThoiKhoaBieuDTO()
            Dim tkbBUS As New ThoiKhoaBieuBUS()
            Dim kq As Integer = -1

            tkbDTO.MaTKB = txtMaTKB.Text.Trim()
            tkbDTO.Thu = cbThu.SelectedItem.ToString().Trim()
            tkbDTO.Buoi = cbBuoi.SelectedItem.ToString().Trim()
            tkbDTO.MaLop = txtMaLop.Text.Trim()

            For i As Integer = 0 To dgLopHoc.Rows.Count - 2
                If (txtMaLop.Text.Trim() = dgLopHoc.Rows(i).Cells(1).Value.ToString().Trim()) Then
                    tkbDTO.TenLop = dgLopHoc.Rows(i).Cells(2).Value.ToString().Trim()
                    Exit For
                End If
            Next

            kq = tkbBUS.ThemThoiKhoaBieu(tkbDTO)

            If kq > 0 Then
                LayDSTKB()
            Else
                MessageBox.Show("Them that bai.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If

        Catch ex As Exception
            If (ex.Message.IndexOf("create duplicate values in the index, primary key, or relationship") > 0) Then
                MessageBox.Show("Ma TKB da ton tai.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Else
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        End Try
    End Sub

    Private Sub btXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btXoa.Click
        Try
            If (txtMaTKB.Text.Trim.Length = 0) Then
                MessageBox.Show("Nhap Ma TKB muon Xoa.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Return
            End If

            Dim tkbDTO As New ThoiKhoaBieuDTO()
            Dim tkbBUS As New ThoiKhoaBieuBUS()
            Dim kq As Integer = -1

            tkbDTO.MaTKB = txtMaTKB.Text.Trim()
            kq = tkbBUS.XoaThoiKhoaBieu(tkbDTO)

            If kq > 0 Then
                LayDSTKB()
            Else
                MessageBox.Show("Xoa that bai.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub btSua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub btCapNhat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btCapNhat.Click
        Try
            If (txtMaTKB.Text.Trim.Length = 0) Then
                MessageBox.Show("Nhap Ma TKB.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Return
            ElseIf (cbThu.SelectedItem = Nothing) Then
                MessageBox.Show("Chon Thu.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Return
            ElseIf (cbBuoi.SelectedItem = Nothing) Then
                MessageBox.Show("Chon Buoi.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Return
            End If

            Dim tkbDTO As New ThoiKhoaBieuDTO()
            Dim tkbBUS As New ThoiKhoaBieuBUS()
            Dim kq As Integer = -1

            tkbDTO.MaTKB = txtMaTKB.Text.Trim()
            tkbDTO.Thu = cbThu.SelectedItem.ToString().Trim()
            tkbDTO.Buoi = cbBuoi.SelectedItem.ToString().Trim()
            tkbDTO.MaLop = txtMaLop.Text.Trim()

            For i As Integer = 0 To dgLopHoc.Rows.Count - 2
                If (txtMaLop.Text.Trim() = dgLopHoc.Rows(i).Cells(1).Value.ToString().Trim()) Then
                    tkbDTO.TenLop = dgLopHoc.Rows(i).Cells(2).Value.ToString().Trim()
                    Exit For
                End If
            Next

            kq = tkbBUS.SuaThoiKhoaBieu(tkbDTO)

            If kq > 0 Then
                LayDSTKB()
            Else
                MessageBox.Show("Cap Nhat that bai.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub btTim_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btTim.Click
        Try
            Dim tkbDTO As New ThoiKhoaBieuDTO()
            Dim tkbBUS As New ThoiKhoaBieuBUS()
            Dim kq As Integer = -1

            tkbDTO.MaTKB = txtMaTKB.Text.Trim()
            If (cbThu.SelectedItem <> Nothing) Then
                tkbDTO.Thu = cbThu.SelectedItem.ToString().Trim()
            End If
            If (cbBuoi.SelectedItem <> Nothing) Then
                tkbDTO.Buoi = cbBuoi.SelectedItem.ToString().Trim()
            End If

            Dim ds As New List(Of ThoiKhoaBieuDTO)
            Dim i As Integer = 1

            ds = tkbBUS.TraCuuTKB(tkbDTO)
            dgTKB.Rows.Clear()

            For Each tkb As ThoiKhoaBieuDTO In ds
                dgTKB.Rows.Add(i, tkb.MaTKB, tkb.Thu, tkb.Buoi, tkb.MaLop, tkb.TenLop)
                i += 1
            Next

        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub btXoaFrm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btXoaFrm.Click
        txtMaTKB.Text = ""
        cbBuoi.SelectedItem = ""
        cbThu.SelectedItem = ""
        txtMaLop.Text = ""
        txtTenLop.Text = ""
    End Sub

    Private Sub btThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btThoat.Click
        Me.Close()
    End Sub

    Private Sub dgTKB_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgTKB.SelectionChanged
        Try
            If (dgTKB.SelectedRows.Count > 0) Then
                If (dgTKB.SelectedRows(0).Cells(1).Value = Nothing) Then
                    Return
                End If

                txtMaTKB.Text = dgTKB.SelectedRows(0).Cells(1).Value.ToString().Trim()
                cbThu.SelectedItem = dgTKB.SelectedRows(0).Cells(2).Value.ToString().Trim()
                cbBuoi.SelectedItem = dgTKB.SelectedRows(0).Cells(3).Value.ToString().Trim()
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub btTinLopHoc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btTinLopHoc.Click
        Try
            Dim lhDTO As New LopHocDTO()
            Dim lhBUS As New LopHocBUS()
            Dim kq As Integer = -1

            lhDTO.TenLop = txtTenLop.Text.Trim()
            If (cbKhoi.SelectedItem <> Nothing) Then
                lhDTO.Khoi = cbKhoi.SelectedItem.ToString().Trim()
            End If

            Dim ds As New List(Of LopHocDTO)
            Dim i As Integer = 1

            ds = lhBUS.TraCuuLopHoc(lhDTO)
            dgLopHoc.Rows.Clear()

            For Each lh As LopHocDTO In ds
                dgLopHoc.Rows.Add(i, lh.MaLop, lh.TenLop, lh.Khoi)
                i += 1
            Next

        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub dgLopHoc_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgLopHoc.SelectionChanged
        Try
            If (dgLopHoc.SelectedRows.Count > 0) Then
                If (dgLopHoc.SelectedRows(0).Cells(1).Value = Nothing) Then
                    Return
                End If

                txtMaLop.Text = dgLopHoc.SelectedRows(0).Cells(1).Value.ToString().Trim()
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub
End Class